# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ingo Maindorfer <ingo@liquidcooling.de>
# Based on work by AlexWMF <alexxwmf@gmail.com>

_realname=qwt
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-qt5"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-qt6")
pkgver=6.2.0
pkgrel=5
pkgdesc="Qt Widgets for Technical Applications (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=("custom:${_realname}")
url="https://qwt.sourceforge.io/"
makedepends=("${MINGW_PACKAGE_PREFIX}-qt5-base"
             "${MINGW_PACKAGE_PREFIX}-qt5-svg"
             "${MINGW_PACKAGE_PREFIX}-qt5-tools"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-svg"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-cc")
options=(!strip staticlibs !buildflags)
source=("https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.bz2"
        001-separate-debug-info-build.patch
        002-link-to-release.patch
        003-qwtconfig-msys2.patch
        004-pkgconfig-file.patch
        005-rename-qwt-to-remove-qt5-qt6-conflict.patch)
sha256sums=('9194f6513955d0fd7300f67158175064460197abab1a92fa127a67a4b0b71530'
            'ef39e5669cb6c0441cdb1fe20c3e51a706bfc3eb7f572a3c0a1dde362b0d40a4'
            '6e0101d2c2897c94da6284ba0b929a7a39d82120313e6cd6353fd5e3aaf4b059'
            '92c49c72bc4fc241157dd9662082b0d2bf35f7bdb7de2778632a82f498ee3871'
            '442570bf827731c83027451f76b23f4b3ffbf0266d02d507c573ca047b06fc18'
            '1c8628fb33e151b6aa33e10db76d81f740ea2a65e75c2bc7c6bd287e996bd77f')

prepare() {
  cd ${_realname}-${pkgver}

  patch -p1 -i ${srcdir}/001-separate-debug-info-build.patch
  patch -p1 -i ${srcdir}/002-link-to-release.patch
  patch -p1 -i ${srcdir}/003-qwtconfig-msys2.patch
  patch -p1 -i ${srcdir}/004-pkgconfig-file.patch
  patch -p1 -i ${srcdir}/005-rename-qwt-to-remove-qt5-qt6-conflict.patch
}

build() {
  # qwt-qt5
  [[ -d ${srcdir}/build-${MSYSTEM}-qt5 ]] && rm -rf ${srcdir}/build-${MSYSTEM}-qt5
  cp -rf ${srcdir}/${_realname}-${pkgver} ${srcdir}/build-${MSYSTEM}-qt5

  cd ${srcdir}/build-${MSYSTEM}-qt5

  local PREFIX_WIN_QT5=$(cygpath -am ${pkgdirbase}/${MINGW_PACKAGE_PREFIX}-qwt-qt5${MINGW_PREFIX})
  sed -s "s|%VALUE_QWT_INSTALL_PREFIX%|${PREFIX_WIN_QT5}|g" -i qwtconfig.pri
  ${MINGW_PREFIX}/bin/qmake CONFIG-=debug_and_release CONFIG+=release qwt.pro

  make

  # qwt-qt6
  [[ -d ${srcdir}/build-${MSYSTEM}-qt6 ]] && rm -rf ${srcdir}/build-${MSYSTEM}-qt6
  cp -rf ${srcdir}/${_realname}-${pkgver} ${srcdir}/build-${MSYSTEM}-qt6

  cd ${srcdir}/build-${MSYSTEM}-qt6

  local PREFIX_WIN_QT6=$(cygpath -am ${pkgdirbase}/${MINGW_PACKAGE_PREFIX}-qwt-qt6${MINGW_PREFIX})
  sed -s "s|%VALUE_QWT_INSTALL_PREFIX%|${PREFIX_WIN_QT6}|g" -i qwtconfig.pri
  ${MINGW_PREFIX}/bin/qmake6 CONFIG-=debug_and_release CONFIG+=release qwt.pro

  make
}

package_qwt-qt5() {
  pkgdesc="Qt Widgets for Technical Applications - Qt5 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt5-base"
           "${MINGW_PACKAGE_PREFIX}-qt5-svg")
  optdepends=("${MINGW_PACKAGE_PREFIX}-qt5-tools")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd ${srcdir}/build-${MSYSTEM}-qt5

  make -j1 install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-qt5/COPYING

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin/
  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.{debug,dll} "${pkgdir}${MINGW_PREFIX}"/bin/

  # install debug info
  #install -Dm644 lib/*.debug -t "${pkgdir}${MINGW_PREFIX}"/bin/

  local PREFIX_WIN=$(cygpath -am ${pkgdir}${MINGW_PREFIX})
  local PREFIX=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/share/qt5/mkspecs/features/qwtconfig.pri
  sed -s "s|qwtAddLibrary(\$\${QWT_INSTALL_LIBS}, qwt)|qwtAddLibrary(\$\${QWT_INSTALL_LIBS}, qwt-qt5)|g" \
    -i ${pkgdir}${MINGW_PREFIX}/share/qt5/mkspecs/features/qwt.prf
  sed -s "s|${PREFIX_WIN}|\$\{prefix\}|g" -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/Qt5Qwt6.pc
  sed -s "s|${PREFIX}|${MINGW_PREFIX}|g" -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/Qt5Qwt6.pc
}

package_qwt-qt6() {
  pkgdesc="Qt Widgets for Technical Applications - Qt6 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-qt6-base"
           "${MINGW_PACKAGE_PREFIX}-qt6-svg")
  optdepends=("${MINGW_PACKAGE_PREFIX}-qt6-tools")

  cd ${srcdir}/build-${MSYSTEM}-qt6

  make -j1 install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}-qt6/COPYING

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin/
  mv "${pkgdir}${MINGW_PREFIX}"/lib/*.{debug,dll} "${pkgdir}${MINGW_PREFIX}"/bin/

  # install debug info
  #install -Dm644 lib/*.debug -t "${pkgdir}${MINGW_PREFIX}"/bin/

  local PREFIX_WIN=$(cygpath -am ${pkgdir}${MINGW_PREFIX})
  local PREFIX=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
    -i ${pkgdir}${MINGW_PREFIX}/share/qt6/mkspecs/features/qwtconfig.pri
  sed -s "s|qwtAddLibrary(\$\${QWT_INSTALL_LIBS}, qwt)|qwtAddLibrary(\$\${QWT_INSTALL_LIBS}, qwt-qt6)|g" \
    -i ${pkgdir}${MINGW_PREFIX}/share/qt6/mkspecs/features/qwt.prf
  sed -s "s|${PREFIX_WIN}|\$\{prefix\}|g" -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/Qt6Qwt6.pc
  sed -s "s|${PREFIX}|${MINGW_PREFIX}|g" -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/Qt6Qwt6.pc
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
